package com.ncmmall.mall.controller.fs;

import cn.hutool.core.io.FileUtil;
import com.feijin.commons.exceptions.NotAllowFileTypeException;
import com.feijin.commons.lang.PropertiesHelps;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 文件上传
 *
 * @author likaikai
 */
@Controller
@RequestMapping(value = "/upload")
@Slf4j
public class UploadController {

    private final static List<String> ALLOW_FILE_TYPE = ImmutableList.of("jpg", "png", "jpeg");

    /**
     * 上传图片
     */
    @RequestMapping(value = "/image", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> image(MultipartFile file) {
        try {

            validType(file.getOriginalFilename());
            String fileName = System.currentTimeMillis() + "." + FileUtil.extName(file.getOriginalFilename());

            File target = new File(PropertiesHelps.getValue("store.path") + File.separator + "uploads", fileName);
            FileUtil.mkParentDirs(target);
            file.transferTo(target);

            return ImmutableMap.of("status", true, "path", fileName);

        } catch (NotAllowFileTypeException e) {
            return ImmutableMap.of("status", false, "msm", "文件不支持");

        } catch (Exception e) {
            log.error("[上传图片信息异常]:", e);
            return ImmutableMap.of("status", false, "msm", "图片信息上传异常");
        }
    }

    /**
     * 上传头像
     */
    @RequestMapping(value = "/avatar", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> avatar(MultipartHttpServletRequest request) {
        try {

            MultipartFile file = request.getFile("file");
            validType(file.getOriginalFilename());
            String fileName = System.currentTimeMillis() + "." + FileUtil.extName(file.getOriginalFilename());

            File target = new File(PropertiesHelps.getValue("store.path") + File.separator + "avatar", fileName);
            FileUtil.mkParentDirs(target);
            file.transferTo(target);

            return ImmutableMap.of("status", true, "path", fileName);

        } catch (NotAllowFileTypeException e) {
            return ImmutableMap.of("status", false, "msm", "文件不支持");

        } catch (Exception e) {
            log.error("[上传图片信息异常]:", e);
            return ImmutableMap.of("status", false, "msm", "图片信息上传异常");
        }
    }


    /**
     * 文件类型校验
     */
    private void validType(String name) {
        String extension = FileUtil.extName(name).toLowerCase();
        if (!ALLOW_FILE_TYPE.contains(extension)) {
            throw new NotAllowFileTypeException();
        }
    }

}
